package com.md.util.system;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.md.base.exception.ServerException;
import com.md.config.aliyun.sms.SmsConfig;
import com.md.util.ObjectUtil;
import com.md.util.StringUtil;
import com.md.value.enums.AliYunSmsEnum;
import com.md.value.enums.ErrorEnum;
import com.md.value.enums.SmsEnum;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;


@UtilityClass
@Slf4j
public class SmsUtils {
  public void sendSms(final SmsEnum type, final String phone, final String content, final String config) {
    if (type == SmsEnum.ALI_YUN) {
      SmsConfig smsConfig = StringUtil.jsonToObject(config, SmsConfig.class);
      SendSmsResponse smsResponse = AliYunUtils.sendSmsCode(smsConfig, phone, content);
      String code = smsResponse.getBody().getCode();
      AliYunSmsEnum of = AliYunSmsEnum.of(code);
      if (ObjectUtil.isEmpty(of) || ObjectUtil.unEquals(of, AliYunSmsEnum.OK)) {
        log.error("发送阿里云短信失败{}: {}", StringUtil.objectToJson(smsResponse), of.getMessage());
        throw ServerException.build(ErrorEnum.SMS_SEND_ERROR);
      }
    }
  }
}
